package odps;

import com.aliyun.odps.Column;
import com.aliyun.odps.Instance;
import com.aliyun.odps.Odps;
import com.aliyun.odps.OdpsException;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;

import java.util.Arrays;
import java.util.List;
/**
 <dependency>
   <groupId>com.aliyun.odps</groupId>
   <artifactId>odps-sdk-core</artifactId>
   <version>0.36.7-public</version>
</dependency>
**/
public class odpsTest {
    public static void main(String[] args) throws OdpsException {
        String accessId="**";
        String accessKey="**";
        String endPoint="http://***/api";
        String project="**";
        Account account = new AliyunAccount(accessId, accessKey);
        Odps odps = new Odps(account);
        odps.setEndpoint(endPoint);
        odps.setDefaultProject(project);
        Instance i = SQLTask.run(odps,
                "select * from **.**;");
        i.waitForSuccess();
        List<Record> records = SQLTask.getResult(i);

        if(!records.isEmpty()) {
            Column[] columns = records.stream().findFirst().get().getColumns();
            records.forEach(r -> {
                Arrays.stream(columns).forEach(column -> {
                    System.out.println(column.getName());//字段名称
                    System.out.println(r.get(column.getName()));//字段数据
                });
            });


        }
    }
}
